package com.wxtf.platform.plugins.permissions;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.FormatUtil;

/**
 * <li>Title: DesktopAction.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.plugins.permissions</li>
 * <li>Description: 个人桌面设置</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Sep 4, 2012 1:39:00 PM</li>
 * 
 * @author chun_chang
 * @version 1.0.0.0
 * 
 */
public class DesktopAction extends LogicBase implements CommonsCallProvider{
	private static Log log = LogFactory.getLog(DesktopAction.class);
	private int _desktopMenuCount = 18;
	public DesktopAction(String jsonArray){

	}

	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {
				msg = this.select(this.getLogicData());
			} else if("listDesktop".equals(this.getMethodName())) {
				msg = this.listDesktop(this.getLogicData());
			} else if("save".equals(this.getMethodName())) {
				msg = this.insert(this.getLogicData());
			} else if("delete".equals(this.getMethodName())) {
				msg = this.delete(this.getLogicData());
			} else if("getNoticeAndAnnouncement".equals(this.getMethodName())) {
				msg = this.getNoticeAndAnnouncement();
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}
			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")) {
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		Object[] params = { user.getUserId() };
		
		String sql11 = "select a.permission_id from t_permission a,t_user_permission b where a.permission_id = b.permission_id and b.user_id = ?";
		String sql12 = "select c.permission_id from t_permission c,t_role_permission d,t_role e,t_user_role f where c.permission_id = d.permission_id and d.role_id = e.role_id and e.role_id = f.role_id and f.user_id = ?";
		String sql13 = "select h.permission_id from t_permission h,t_role_permission i,t_role j,t_group_role k,t_group l,t_user_group m where h.permission_id = i.permission_id and i.role_id = j.role_id and j.role_id = k.role_id and k.group_id = l.group_id and l.group_id = m.group_id and m.user_id = ?";
		
		//菜单
		List<Object> list2 = new ArrayList<Object>();
		List<Object> list1 = AccessDataBaseUtil.query4List(sql11, params);
		list2.addAll(list1);
		//角色
		list1 = AccessDataBaseUtil.query4List(sql12, params);
		list2.addAll(list1);
		
		//组
		list1 = AccessDataBaseUtil.query4List(sql13, params);//当前节点的菜单ID
		list2.addAll(list1);
		
		String ids = "";
		if (list2.size() > 0) {
			for (Object obj : list2) {
				ids += "'" + obj + "',";
			}
		}
		ids += "''";
		
		sql = " from ( select o.permission_id,o.caption,o.action,o.quickicon,o.showorder from t_permission o where o.isquick = 'y' and o.action is not null and o.action <> '' and o.permission_id in (" + ids + ") ) r left join t_desktop s on r.permission_id = s.permission_id and s.user_id = ? ";
		
		// 排序
		String sortname = this.getRequest().getParameter("sortname");
		String sortorder = this.getRequest().getParameter("sortorder");
		if(sortname != null && sortname != null) {
			sql += " order by r." + sortname + " " + sortorder;
		}
		
		String countSql = "select count(*) " + sql;
		String querySql = "";
		if("mysql".equals(user.getDbType())){
			querySql = "select s.ordernums,s.user_id,r.permission_id,r.caption,r.action,r.quickicon,r.showorder " + sql + " order by ifnull(s.ordernums, 999),r.showorder asc";
		} else if("oracle".equals(user.getDbType())){
			querySql = "select decode(s.ordernums,'',0,s.ordernums),s.user_id,r.permission_id,r.caption,r.action,r.quickicon,r.showorder " + sql + " order by s.ordernums,r.showorder asc";
		}
		String[] fields = { "ordernums", "userId", "id", "text", "action", "quickIcon","showorder" };
		return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), params);
	}

	/** 
	 * 描述：listDesktop 桌面快捷菜单列表
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @CreateOn Sep 5, 2012  10:44:59 AM
	 * @author chun_chang
	 */
	public Object listDesktop(Object jsonArray) throws Exception {
		UserBean user = (UserBean) this.getRequest().getSession().getAttribute("userBean");
		String querySql = "select r.permission_id,r.caption,r.action,r.quickicon,r.types from t_permission r, t_desktop s where r.permission_id = s.permission_id and s.user_id = ? order by s.ordernums asc";
		String[] jsonField = { "id", "name", "action", "icon", "types" };
		Object[] params = { user.getUserId() };
		List<Map<String, String>> list = AccessDataBaseUtil.query(jsonField, querySql, params);
		JSONArray array = JSONArray.fromObject(list);
		String json = array.toString();
		//[{visit1:true,visit2:true,visit3:true},{data:[]}]
		int cnt1 = 0, cnt2 = 0, cnt3 = 0;
		
		sql = "select count(u.geshu) as shu from ( select (a.tmamvm_lmedicine_time-(case when g.num is null then 0 else g.num end)) as geshu from (select tmamvm_id,org_id,tmamvm_lmedicine_time from tma_medicine_vistor_main group by tbe_id order by tmamvm_date desc) a left join (select count(*) as num,f.tmamvm_id from (select distinct(substr(tmamvd_time,1,7)),tmamvm_id from tma_medicine_vistor_detail) f group by f.tmamvm_id) g on g.tmamvm_id=a.tmamvm_id where a.org_id = ? ) u where u.geshu>0";
		cnt1 = AccessDataBaseUtil.queryForInt(sql, new Object[]{ user.getCurOrgId() });
		
		if(!this.keyserobj_yuqi_yesorno()){
			cnt2 = 1;
		}
		
		if(!this.sishu_yuqi()) {
			cnt3 = 1;
		}
		
		json = "[{\"visit1\":\"" + cnt1 + "\",\"visit2\":\"" + cnt2 + "\",\"visit3\":\"" + cnt3 + "\",\"data\":" + json + "}]";
		return json;
	}
	
	/** 
	 * 描述：sishu_yuqi() 判断四术逾期
	 * @param 
	 * @return true(不逾期) false(逾期)
	 * @CreateOn Oct 29, 2012  4:02:00 PM
	 * @author wangzhi
	 */
	private boolean sishu_yuqi() throws Exception{
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		String surTime_sql="select SC_VALUE from t_system_config where SC_KEY='surTime' and SC_UID='0' "; 
		int surTime=Integer.valueOf (AccessDataBaseUtil.query4List(surTime_sql, null).toString().replace("[", "").replace("]", "").replace(" ", "")).intValue();//逾期标准（日）
		
		String sql = "select tbcsr_time from tbc_surgery_record where tbcsr_id not in (select tbcsr_id from tbc_surgery_record_vr)  and org_id in (" + user.getCurSubOrgIds(user.getCurOrgId()) + ") order by tbcsr_time asc;";
		String tbcsr_time_val = AccessDataBaseUtil.query4List(sql, null).toString().replace("[", "").replace("]", "").replace(" ", "");
		
		if("".equals(tbcsr_time_val)||null==tbcsr_time_val||" ".equals(tbcsr_time_val))
		{return true;}
		
		String []shoushu_time= tbcsr_time_val.split(",");
		String []nowTime = FormatUtil.getCurentDate().split("-");//[]nowTime   现在时间
		
		for(int i = 0; i < shoushu_time.length; i++) {
			if("".equals(shoushu_time[i])||null==shoushu_time[i]||" ".equals(shoushu_time[i]))
			{continue;}
			
			String []shoushuTime=shoushu_time[i].split("-");//[]shoushu_time   手术时间
			if(Integer.valueOf(shoushuTime[0]).intValue()==Integer.valueOf(nowTime[0]).intValue())  //手术时间年=当前年
			{
				int mon_tem=Integer.valueOf(nowTime[1]).intValue()-Integer.valueOf(shoushuTime[1]).intValue();
				if(mon_tem>1)//大于一个月
				{
					return false;
				}
				else if(mon_tem==1&&Integer.valueOf(nowTime[2])>=surTime)//一个月 当前日已经>=规则日
				{
					return false;
				}
			}
			else if(Integer.valueOf(shoushuTime[0]).intValue()<Integer.valueOf(nowTime[0]).intValue())  //手术时间年<当前年
			{
				return false;
			}
		}
		return true;
	}
	
	@Override
	public Object insert(Object jsonArray) throws Exception {
		UserBean user = (UserBean) this.getRequest().getSession().getAttribute("userBean");
		sql = "select permission_id from t_desktop where user_id = ?";
		List<Object> listAll = AccessDataBaseUtil.query4List(sql, new Object[] { user.getUserId() });
		Set<String> set = new HashSet<String>();
		for(Object obj: listAll) {
			set.add(obj.toString());
		}
		
		List<String> list = new ArrayList<String>();
		StringBuffer buff = new StringBuffer();
		JSONArray array = JSONArray.fromObject(jsonArray);
		int size = set.size();
		
		for(int i = 0; i < array.size(); i++) {
			if(size >= _desktopMenuCount){
				break;
			}
			size++;
			JSONObject jsonObject = JSONObject.fromObject(array.getString(i));
			String id = jsonObject.getString("id");
			String num = jsonObject.getString("num");
			if(set.add(id)){//不存在就保存
				set.remove(id);
				buff.append("insert into t_desktop(desktop_id,user_id,permission_id,ordernums)")
					.append(" values(")
					.append("'").append(AccessDataBaseUtil.getPKUUID()).append("',")
					.append("'").append(user.getUserId()).append("',")
					.append("'").append(id).append("',")
					.append(num).append(")");
			} else {//存在更新排序号
				buff.append("update t_desktop set ordernums = ").append(num)
					.append(" where user_id = '").append(user.getUserId()).append("'")
					.append(" and permission_id = '").append(id).append("'");
			}
			
			list.add(buff.toString());
			buff.setLength(0);
		}
		if(list.size() > 0){
			AccessDataBaseUtil.executeSQLUpdate(list);
		}
		if(size >= _desktopMenuCount){
			return "\"快捷菜单最多18个，已经存在了"+set.size()+"条，故本次保存了"+(_desktopMenuCount - set.size())+"条记录\"";
		}
		return "\"保存成功\"";
	}

	@Override
	public Object update(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object delete(Object jsonArray) throws Exception {
		UserBean user = (UserBean) this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String ids = jsonObject.getString("ids");
		sql = "delete from t_desktop where USER_ID = '" + user.getUserId() + "' and PERMISSION_ID in ('" + ids + "')";
		AccessDataBaseUtil.executeSQLUpdate(sql);
		return "\"删除成功\"";
	}
	
	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}

	/**
	 * 查询通知和公告
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author bai_xin
	 */
	public Object getNoticeAndAnnouncement() throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		Map<String, Object> result = new HashMap<String, Object>();
		//查询公告信息
		//首先将所有超时公告设置为失效
		StringBuffer announcementTimeSql = new StringBuffer();
		if("mysql".equals(user.getDbType())){
			announcementTimeSql.append("update tm_announcement set tma_status=0 where tma_status=1 and tma_time!=0 and current_timestamp() > DATE_ADD(CREATE_TIME,INTERVAL tma_time HOUR)");
		}else if("oracle".equals(user.getDbType())){
			announcementTimeSql.append("update tm_announcement set tma_status=0 where tma_status=1 and tma_time!=0 and to_date(sysdate,'yyyy-mm-dd hh24:mi:ss') > to_date(CREATE_TIME+tma_time/24,'yyyy-mm-dd hh24:mi:ss')");
		}
		AccessDataBaseUtil.executeSQLUpdate(announcementTimeSql.toString());
		//获取仍然有效的公告
		String[] announcementFields = {"tma_from","tma_content","create_by","create_time"};
		StringBuffer announcementSql = new StringBuffer("select TMA_FROM,TMA_CONTENT,CREATE_BY,CREATE_TIME from TM_ANNOUNCEMENT where TMA_STATUS=1 order by CREATE_TIME desc");
		List<Map<String, Object>> announcementList = AccessDataBaseUtil.query4ListMap(announcementFields, announcementSql.toString(), null);
		//判断是否还有公告
		if(announcementList != null && announcementList.size() > 0){
			result.put("announcements", announcementList);
		}
		
		//查询通知信息
		//首先将所有超时通知设置为失效
		StringBuffer noticeTimeSql = new StringBuffer();
		if("mysql".equals(user.getDbType())){
			noticeTimeSql.append("update tm_notice set tmn_status=0 where tmn_to='").append(user.getCurOrgId()).append("' and tmn_status=1 and tmn_time!=0 and current_timestamp() > DATE_ADD(CREATE_TIME,INTERVAL tmn_time HOUR)");
		}else if("oracle".equals(user.getDbType())){
			noticeTimeSql.append("update tm_notice set tmn_status=0 where tmn_to='").append(user.getCurOrgId()).append("' and tmn_status=1 and tmn_time!=0 and to_date(sysdate,'yyyy-mm-dd hh24:mi:ss') > to_date(CREATE_TIME+tmn_time/24,'yyyy-mm-dd hh24:mi:ss')");
		}
		AccessDataBaseUtil.executeSQLUpdate(noticeTimeSql.toString());
		
		//获取仍然有效的通知
		String[] noticeFields = {"tmn_from","tmn_to","tmn_content","create_by","create_time"};
		StringBuffer noticeSql = new StringBuffer("select TMN_FROM,TMN_TO,TMN_CONTENT,CREATE_BY,CREATE_TIME from TM_NOTICE where TMN_TO='").append(user.getCurOrgId()).append("' and TMN_STATUS=1 order by CREATE_TIME desc");
		List<Map<String, Object>> noticeList = AccessDataBaseUtil.query4ListMap(noticeFields, noticeSql.toString(), null);
		//判断是否还有通知
		if(noticeList != null && noticeList.size() > 0){
			//将一次性通知（即：tmn_time为0）设置为失效
			String updateStatusSql = "update TM_NOTICE set TMN_STATUS=0 where TMN_TO='" + user.getCurOrgId() + "' and tmn_status=1 and tmn_time=0";
			AccessDataBaseUtil.executeSQLUpdate(updateStatusSql);
			result.put("notices", noticeList);
		}
		if(result.size() > 0){
			return JSONArray.fromObject(result);
		}else{
			return false;
		}
	}
	
	/**
	 * 查询公告
	 * @param jsonArray
	 * @return
	 * @throws Exception
	 * @author bai_xin
	 */
	public Object getAnnouncement() throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		//首先将所有超时通知设置为失效
		StringBuffer timeSql = new StringBuffer();
		if("mysql".equals(user.getDbType())){
			timeSql.append("update tm_announcement set tma_status=0 where tma_status=1 and tma_time!=0 and current_timestamp() > DATE_ADD(CREATE_TIME,INTERVAL tma_time HOUR)");
		}else if("oracle".equals(user.getDbType())){
			timeSql.append("update tm_announcement set tma_status=0 where tma_status=1 and tma_time!=0 and to_date(sysdate,'yyyy-mm-dd hh24:mi:ss') > to_date(CREATE_TIME+tma_time/24,'yyyy-mm-dd hh24:mi:ss')");
		}
		AccessDataBaseUtil.executeSQLUpdate(timeSql.toString());
		
		//获取仍然有效的通知
		String[] noticeFields = {"tma_from","tma_type","tma_content","create_by","create_time"};
		StringBuffer noticeSql = new StringBuffer("select TMA_FROM,TMA_TYPE,TMA_CONTENT,CREATE_BY,CREATE_TIME from TM_ANNOUNCEMENT where TMA_STATUS=1");
		List<Map<String, Object>> noticeList = AccessDataBaseUtil.query4ListMap(noticeFields, noticeSql.toString(), null);
		//判断是否还有通知
		if(noticeList != null && noticeList.size() > 0){
			//将一次性通知（即：tmn_time为0）设置为失效
			String updateStatusSql = "update TM_ANNOUNCEMENT set TMA_STATUS=0 where tma_status=1 and tma_time=0";
			AccessDataBaseUtil.executeSQLUpdate(updateStatusSql);
			JSONArray array = JSONArray.fromObject(noticeList);
			return array.toString();
		}
		
		return false;
	}
	
	// 判断某月是第几季度
	public static String season_from_month(String month){
		String jidu = "";
		if("01".equals(month)) {
			jidu = "1";
		}
		if("02".equals(month)) {
			jidu = "1";
		}
		if("03".equals(month)) {
			jidu = "1";
		}
		if("04".equals(month)) {
			jidu = "2";
		}
		if("05".equals(month)) {
			jidu = "2";
		}
		if("06".equals(month)) {
			jidu = "2";
		}
		if("07".equals(month)) {
			jidu = "3";
		}
		if("08".equals(month)) {
			jidu = "3";
		}
		if("09".equals(month)) {
			jidu = "3";
		}
		if("10".equals(month)) {
			jidu = "4";
		}
		if("11".equals(month)) {
			jidu = "4";
		}
		if("12".equals(month)) {
			jidu = "4";
		}
		return jidu;
	}

	// 根据季度得到该季度首月跟末月
	public static int yue_from_season(int season, int flag){
		int a = 0;
		if(flag == 0) {
			if(season == 1) {
				a = 1;
			}
			if(season == 2) {
				a = 4;
			}
			if(season == 3) {
				a = 7;
			}
			if(season == 4) {
				a = 10;
			}
		}
		if(flag == 1) {
			if(season == 1) {
				a = 3;
			}
			if(season == 2) {
				a = 6;
			}
			if(season == 3) {
				a = 9;
			}
			if(season == 4) {
				a = 12;
			}
		}
		return a;
	}
	
	/** 
	 * 描述：keyserobj_yuqi_yesorno() 根据当前用户的机构来判断其机构及下属机构是否有重点服务对象逾期
	 * @param 
	 * @return true(不逾期) false(逾期)
	 * @CreateOn Oct 12, 2012  4:45:00 PM
	 * @author wangzhi
	 */
	private boolean keyserobj_yuqi_yesorno() throws Exception{
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		String sql = "select tbe_id from tb_employee where tbe_is_es='1' and tbe_employment_forms <> '31' and tbe_status not in ('102-3','102-4','102-5') and org_id in (" + user.getCurSubOrgIds(user.getCurOrgId()) + ") ;";
		String peo_id = AccessDataBaseUtil.query4List(sql, null).toString().replace("[", "").replace("]", "").replace(" ", "");
		if(peo_id.length()>0){
			
		String[] uid = peo_id.split(",");
		for(int i = 0; i < uid.length; i++) {
			String tbe_id = uid[i];// 获取职员ID
			String jidu = "";// 现在的季度
			String curTime = FormatUtil.getCurentDate(); // yyyy-mm-dd
			String[] a = curTime.split("-");
			String u_month = a[1];			
			jidu=season_from_month(u_month);		
			String sql_find_mmdd = " select SC_VALUE from t_system_config  where SC_UID = '0' and SC_KEY ='" + jidu + "' ";
			String data_role = AccessDataBaseUtil.query4List(sql_find_mmdd, null).toString().replace("[", "").replace("]", "");
			String u_year = a[0];
			String yyyy_mm_dd = "";
			yyyy_mm_dd = u_year + "-" + data_role; // 得到的逾期提醒时间，yyyy-mm-dd

			String sql_exite = "";
			int row_val = 0;
			
			String sql_select_keytime = "select tbe_es_time from tb_employee where tbe_id='" + tbe_id + "' ";
			String keytime = AccessDataBaseUtil.query4List(sql_select_keytime, null).toString().replace("[", "").replace("]", "");// 设为重点时间
			if("".equals(keytime)||" ".equals(keytime)||keytime==null){continue;} //判断设为重点时间是否存在
			String[] keytime_arry = keytime.split("-");
			String sql_role_time_from_keytime = "select SC_VALUE from t_system_config  where SC_UID = '0' and SC_KEY ='" + season_from_month(keytime_arry[1]) + "' ";
			String time_role_from_keytime = AccessDataBaseUtil.query4List(sql_role_time_from_keytime, null).toString().replace("[", "").replace("]", "");
			String keytime_yyyy_mm_dd=u_year + "-" + time_role_from_keytime; //根据设为重点对象时间 得到的逾期提醒时间，yyyy-mm-dd
			
			if(Integer.valueOf(keytime_arry[0]).intValue() == Integer.valueOf(u_year).intValue()) {
				if(keytime.compareTo(keytime_yyyy_mm_dd) < 0) {
					if(curTime.compareTo(yyyy_mm_dd) < 0) {
						int season_from=(Integer.valueOf(season_from_month(keytime_arry[1])).intValue());
						int season_to=(Integer.valueOf(season_from_month(u_month)).intValue()-2);
						sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue())+"%' and  month(t3.tesvr_time) >= '"+yue_from_season(season_from,0)+"' and month(t3.tesvr_time) <= '"+yue_from_season(season_to,1)+"' "; 
						row_val=season_to-season_from+1;
					} else if(curTime.compareTo(yyyy_mm_dd) >= 0) {
						int season_from=(Integer.valueOf(season_from_month(keytime_arry[1])).intValue());
						int season_to=(Integer.valueOf(season_from_month(u_month)).intValue()-1);
						sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue())+"%' and  month(t3.tesvr_time) >= '"+yue_from_season(season_from,0)+"' and month(t3.tesvr_time) <= '"+yue_from_season(season_to,1)+"' "; 
						row_val=season_to-season_from+1;
					}
				} else if(keytime.compareTo(keytime_yyyy_mm_dd) >= 0) {
					if(curTime.compareTo(yyyy_mm_dd) < 0) {
						int season_from=(Integer.valueOf(season_from_month(keytime_arry[1])).intValue());
						int season_to=(Integer.valueOf(season_from_month(u_month)).intValue()-2);
						sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue())+"%' and  month(t3.tesvr_time) >= '"+yue_from_season(season_from,0)+"' and month(t3.tesvr_time) <= '"+yue_from_season(season_to,1)+"' "; 
						row_val=season_to-season_from+1;
					} else if(curTime.compareTo(yyyy_mm_dd) >= 0) {
						int season_from=(Integer.valueOf(season_from_month(keytime_arry[1])).intValue());
						int season_to=(Integer.valueOf(season_from_month(u_month)).intValue()-1);
						sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue())+"%' and  month(t3.tesvr_time) >= '"+yue_from_season(season_from,0)+"' and month(t3.tesvr_time) <= '"+yue_from_season(season_to,1)+"' "; 
						row_val=season_to-season_from+1;
					}
				}
			} else if(Integer.valueOf(keytime_arry[0]).intValue() < Integer.valueOf(u_year).intValue()) {
				if(curTime.compareTo(yyyy_mm_dd) < 0) {
					if("1".equals(jidu))  
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue()-1)+"%' and  t3.reserve1 in ('第一季度','第二季度','第三季度') ";
					row_val=3;}
					if("2".equals(jidu))  
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue()-1)+"%' and  t3.reserve1 in ('第一季度','第二季度','第三季度','第四季度') ";
					row_val=4;}
					if("3".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+u_year+"%' and  t3.reserve1 in ('第一季度') ";
					row_val=1;}
					if("4".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+u_year+"%' and  t3.reserve1 in ('第一季度','第二季度') ";
					row_val=2;}
				}
				if(curTime.compareTo(yyyy_mm_dd) >= 0) {
					if("1".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+( Integer.valueOf(u_year).intValue()-1)+"%' and  t3.reserve1 in ('第一季度','第二季度','第三季度','第四季度') "; row_val=4;}
					if("2".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+u_year+"%' and  t3.reserve1 in ('第一季度')  "; row_val=1;}
					if("3".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+u_year+"%' and  t3.reserve1 in ('第一季度','第二季度') ";row_val=2;}
					if("4".equals(jidu))
					{sql_exite="select count(*) from tb_employee t1 left join tb_family_base t2 on t1.tbe_id=t2.tbe_id left join tes_vistor_record t3 on t3.tbfi_id=t2.tbfi_id where t1.tbe_id='"+tbe_id+"' and t3.tesvr_time like '"+u_year+"%' and  t3.reserve1 in ('第一季度','第二季度','第三季度') ";row_val=3;}
				}
			} else if(Integer.valueOf(keytime_arry[0]).intValue() > Integer.valueOf(u_year).intValue()) {
				continue;
			}

			int value = Integer.parseInt(AccessDataBaseUtil.query4List(sql_exite, null).toString().replace("[", "").replace("]", ""));
			if(value < row_val) { // 逾期
				return false;
			}
		}
	}
		return true;
	}
	
}
